/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package cn.nexd.app.guestsystem.ui;

import android.annotation.TargetApi;
import android.os.Build;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.transitionseverywhere.ChangeText;
import com.transitionseverywhere.TransitionManager;

import cn.nexd.app.guestsystem.Configuration;
import cn.nexd.app.guestsystem.R;
import cn.nexd.app.guestsystem.model.User;
import cn.nexd.app.guestsystem.presenter.MainPresenter;
import cn.nexd.app.guestsystem.ui.inter.IMainView;


/*
 * MainActivity class that loads MainFragment
 */
public class MainActivity extends BaseActivity<MainPresenter> implements IMainView {
    /**
     * Called when the activity is first created.
     */
    private static final String TAG = "MainActivity";

    private TextView tvCompany;
    private TextView tvPosition, tvName, tvWelInfo;
    private RelativeLayout rlContainer;
    private boolean userIsNull = true;


    @Override
    public void initPresenter() {
        presenter = new MainPresenter(this, this);
        presenter.init();
    }

    @Override
    public int getLayoutResId() {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        return R.layout.activity_welcome;
    }

    @Override
    public void initView() {
        tvCompany = (TextView) findViewById(R.id.wel_info_company);
        tvPosition = (TextView) findViewById(R.id.wel_info_position);
        tvName = (TextView) findViewById(R.id.wel_info_name);
        rlContainer = (RelativeLayout) findViewById(R.id.wel_rl_container);
        tvWelInfo = (TextView) findViewById(R.id.wel_info_wel);
        presenter.initWhiteList();
        presenter.initCollect();
    }

    @Override
    public void showToast(String msg) {
        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void setShowText(User user) {
        if (user == null && !TextUtils.isEmpty(tvCompany.getText().toString())) {
            if (!userIsNull) {
                tvPosition.setText("");
                tvCompany.setText("");
                tvName.setText("");
                tvWelInfo.setText("");
                tvPosition.setText(R.string.welcom_all_guest);
                userIsNull = true;
                tvPosition.setTextSize(30);
                tvPosition.setLetterSpacing(0.1f);
            }
        } else if (user != null) {
            if (userIsNull) {
                tvPosition.setText("");
                userIsNull = false;
            }
            ChangeText changeText = new ChangeText().setChangeBehavior(ChangeText.CHANGE_BEHAVIOR_OUT_IN);
            TransitionManager.beginDelayedTransition(rlContainer, changeText);
            tvCompany.setText(user.getCompany());
            tvPosition.setText(user.getPosition());
            tvName.setText(user.getFamilyName() + user.getName());
            tvPosition.setTextSize(20);
            tvPosition.setLetterSpacing(0);
            tvWelInfo.setText(Configuration.Config.Greet);
        }
    }

    public void reportMacs(View view) {
        presenter.getAllMac();
    }
}
